package com.zhixianggou.caibaobox.lottery.dataCollect.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zhixianggou.caibaobox.commons.utils.HttpUtils;
import com.zhixianggou.caibaobox.lottery.dataCollect.dto.SLotteryPageDto;
import com.zhixianggou.caibaobox.lottery.dataCollect.dto.WLotteryPageDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.actuate.web.exchanges.HttpExchange;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.http.HttpResponse;

/**
 * @author zyq
 * 8/22/24 5:47 PM
 */
public class DataCollectUtil {

    private static String SPORT_LOTTERY_URL = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=GAMENO&provinceId=0&isVerify=1&pageSize=PAGESIZE&pageNo=PAGENO";
    private static String WELFARE_LOTTERY_URL = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=NAME&pageNo=PAGENO&pageSize=PAGESIZE&systemType=PC";


    public static enum SGameNo{
        S_DALETOU("85"),
        S_ARRAY3("35"),
        S_ARRAY5("350133"),
        S_SEVENSTAR("04"),
        ;
        private String no;
        private String value;
        SGameNo(String no){
            this.no = no;
        }
        public String getNo() {
            return no;
        }
    }

    public static enum WName{
        W_SSQ("ssq"),
        W_KL8("kl8"),
        W_3D("3d"),
        W_QLC("qlc"),
        ;
        private String name;
        WName(String name){
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }


    /**
     *  获取体彩数据
     * @param restTemplate
     * @param gameNo
     * @param pageNo
     * @param pageSize
     * @param startTerm
     * @param endTerm
     * @return
     */
    public static SLotteryPageDto getList4SLottery(RestTemplate restTemplate,String gameNo, Integer pageNo, Integer pageSize, String startTerm, String endTerm)
    {
        String url = SPORT_LOTTERY_URL.replace("GAMENO",gameNo).replace("PAGENO",pageNo.toString()).replace("PAGESIZE",pageSize.toString());
        if(StringUtils.isNotBlank(startTerm) && StringUtils.isNotBlank(endTerm)){
            url = url + "&startTerm=" + startTerm + "&endTerm=" + endTerm;
        }
        String forObject = restTemplate.getForObject(url, String.class);
        SLotteryPageDto page = JSON.parseObject(JSON.toJSONString(JSONObject.parseObject(forObject).get("value")), SLotteryPageDto.class);
        return page;
    }
    public static SLotteryPageDto getList4SLottery(RestTemplate restTemplate,String gameNo, Integer pageNo, Integer pageSize)
    {
        return getList4SLottery(restTemplate,gameNo,pageNo,pageSize,null,null);
    }

    /**
     *  获取福彩数据
     * @param restTemplate
     * @param name
     * @param pageNo
     * @param pageSize
     * @param issueStart
     * @param issueEnd
     * @return
     */
    public static ResponseEntity<String> getList4WLottery(RestTemplate restTemplate,HttpHeaders headers,String name, Integer pageNo, Integer pageSize, String issueStart, String issueEnd)
    {
        String url = WELFARE_LOTTERY_URL.replace("NAME",name).replace("PAGENO",pageNo.toString()).replace("PAGESIZE",pageSize.toString());
        if(StringUtils.isNotBlank(issueStart) && StringUtils.isNotBlank(issueEnd)){
            url = url + "&issueStart=" + issueStart + "&issueEnd=" + issueEnd;
        }

//        String forObject = restTemplate.getForObject(url, String.class);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null, headers), String.class);

        return responseEntity;
    }
    public static ResponseEntity<String> getList4WLottery(RestTemplate restTemplate,HttpHeaders headers,String name, Integer pageNo, Integer pageSize)
    {
        return getList4WLottery(restTemplate,headers,name,pageNo,pageSize,null,null);
    }

    public static void main(String[] args) {
        String url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo=1&pageSize=1&systemType=PC";
//               url = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=NAME&pageNo=PAGENO&pageSize=PAGESIZE&systemType=PC";
//        url = WELFARE_LOTTERY_URL.replace("NAME","ssq").replace("PAGENO","1").replace("PAGESIZE","1");
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        String userAgent = UserAgentUtil.randomUserAgent();
        headers.set("user-agent",userAgent);
//        headers.set("cookie", "HMF_CI=64867bff98953f62e2eecf904ddccfeff939cdfb7fd03c091ffeea52444a8765af09e0169233b6b04d16ea4f9744af7d88dac0781732cc98817851b15c3158e030; 21_vq=17");
//        headers.set("cookie", "HMF_CI=7744fe2242a650efbde96f6bfe41145e35577c105e77c056cad7e540453344365653ef1370d9678b6c575b69c9f0bedbf029531521a6da85033cdca4147320a3be; 21_vq=17");
//        headers.set("cookie", "HMF_CI=565ca5798c7692577e686343ebc41d2510706c7218b54de0b00d3fb821a15a38b4a3c1d6155bffb57babef422076bcb2009209cf0de9e3f983a960164b199a19a4");
//        headers.set("cookie", CookieUtil.getCookie("https://www.cwl.gov.cn") );
        String cookie = CookieUtil.getCookie("https://www.cwl.gov.cn", userAgent);
        headers.set("cookie", cookie);

//        String forObject = restTemplate.getForObject(url, String.class);
//        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null, headers), String.class);

        String string = exchange.toString();
        System.out.println(exchange.getBody());
    }

//    public static void main(String[] args) {
//        HttpHeaders headers = new HttpHeaders();
//        headers.set("cookie", CookieUtil.getCookie("https://www.cwl.gov.cn"));
//        headers.set("user-agent", UserAgentUtil.randomUserAgent());
//        RestTemplate restTemplate = new RestTemplate();
//        //获取网站最近一期数据
//        WLotteryPageDto lastDataPageDto = DataCollectUtil.getList4WLottery(restTemplate,headers, DataCollectUtil.WName.W_SSQ.getName(), 1, 1);
//        System.out.println(lastDataPageDto);
//    }


}
